package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Keyword;
import com.abaaba.project2.bean.KeywordExample;
import com.abaaba.project2.bean.bo.AdminKeywordBo;
import com.abaaba.project2.bean.vo.AdminKeywordVo;
import com.abaaba.project2.mapper.KeywordMapper;
import com.abaaba.project2.service.AdminKeywordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class AdminKeywordServiceImpl implements AdminKeywordService {
    @Autowired
    KeywordMapper keywordMapper;
    @Override
    public AdminKeywordVo getKeywordList(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        PageHelper.startPage(page, limit);
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(sort + " " + order);

        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (url != null && !"".equals(url)) {
            criteria.andUrlLike("%" + url + "%");
        }

        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        PageInfo<Keyword> keywordPageInfo = new PageInfo<>(keywords);
        long total = keywordPageInfo.getTotal();
        AdminKeywordVo adminKeywordVo = new AdminKeywordVo();
        adminKeywordVo.setItems(keywords);
        adminKeywordVo.setTotal(total);
        return adminKeywordVo;
    }

    @Override
    public Integer deleteKeyword(Keyword keyword) {
        keyword.setDeleted(true);
        return keywordMapper.updateByPrimaryKey(keyword);
    }

    @Override
    public Keyword createKeyword(AdminKeywordBo adminKeywordBo) {
        // 将新关键词插入数据库
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Keyword keyword = new Keyword(null, adminKeywordBo.getKeyword(), adminKeywordBo.getUrl(), adminKeywordBo.getIsHot(),
                adminKeywordBo.getIsDefault(), null, simpleDateFormat.format(new Date()), simpleDateFormat.format(new Date()), false);
        int affectedRows = keywordMapper.insertSelective(keyword);

        // 将刚刚插入的数据查询回来
        KeywordExample keywordExample = new KeywordExample();
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        int maxId = keywords.get(0).getId();
        for (Keyword keyword1 : keywords) {
            if (maxId < keyword1.getId()) {
                maxId = keyword1.getId();
            }
        }
        Keyword keyword1 = keywordMapper.selectByPrimaryKey(maxId);
        return keyword1;
    }

    @Override
    public Keyword updateKeyword(Keyword keyword) {
        keyword.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int affectedRows = keywordMapper.updateByPrimaryKey(keyword);

        return keyword;
    }
}
